package com.zx.eval.api.admin;

import java.util.*;

import com.zx.common.utils.Constant;
import com.zx.common.utils.ShiroUtils;
import com.zx.sys.user.api.AbstractApi;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zx.eval.entity.DictExtEntity;
import com.zx.eval.service.DictExtService;
import com.zx.common.utils.PageUtils;
import com.zx.common.utils.R;


/**
 * 字典表
 *
 * @author LiYe@zx
 * @email ily_hxy@163.com
 * @date 2020-03-29 18:17:26
 */
@RestController
@RequestMapping("eval/dictext")
public class DictExtApi {
    @Autowired
    private DictExtService dictExtService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("eval:dictext:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = dictExtService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("eval:dictext:info")
    public R info(@PathVariable("id") Integer id) {
        DictExtEntity dictExt = dictExtService.getById(id);

        return R.ok().put("info", dictExt);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("eval:dictext:save")
    public R save(@RequestBody DictExtEntity dictExt) {
        Long userId = ShiroUtils.getUserId();
        dictExt.setCreateTime(new Date());
        dictExt.setCreator(userId);
        dictExtService.save(dictExt);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("eval:dictext:update")
    public R update(@RequestBody DictExtEntity dictExt) {
        dictExtService.updateById(dictExt);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("eval:dictext:delete")
    public R delete(@RequestBody Integer[] ids) {
        dictExtService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取分类列表
     */
    @RequestMapping("/getCategorys")
    @RequiresPermissions("eval:dictext:list")
    public R getCategorys(@RequestParam Map<String, Object> params) {
        HashMap<Integer, String> categorys = Constant.DEPTMAP;
        ArrayList<Map> maps = new ArrayList<>();
        for (Integer o : categorys.keySet()) {
            String value = categorys.get(o);
            HashMap<String, Object> item = new HashMap<>();
            item.put("name", value);
            item.put("value", o);
            maps.add(item);
        }

        return R.ok().put("categorys", maps);
    }

}
